package com.molichuxing.gateway.bff.statistics.services.impl;

import com.molichuxing.gateway.bff.statistics.services.StatisticsBffService;
import com.molichuxing.services.business.dto.response.VehicleNameBizDto;
import com.molichuxing.services.business.service.VehicleBizService;
import com.molichuxing.services.infrastructure.dto.request.create.CarStatisticsCreateDto;
import com.molichuxing.services.infrastructure.dto.response.CarDto;
import com.molichuxing.services.infrastructure.dto.response.CarStatisticsDto;
import com.molichuxing.services.infrastructure.service.CarService;
import com.molichuxing.services.infrastructure.service.CarStatisticsService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @Author: renzhengyu
 * @Description:
 * @Date: Created in 10:41 2019/10/16
 * @Modified By:
 **/
@Service("statisticsService")
public class StatisticsBffServiceImpl implements StatisticsBffService {

    @Resource
    private CarService carService;
    @Resource
    private CarStatisticsService carStatisticsService;
    @Resource
    private VehicleBizService vehicleBizService;

    @Override
    public Boolean carStatisticsRecord(Integer carId) throws Exception {

        CarDto carDto = carService.getCarById(carId);
        if (null == carDto) {
            throw new Exception("非法入参");
        }
        //根据车型id查询统计表是否有数据
        CarStatisticsDto carStatisticsDto = carStatisticsService.getCarStatisticsByVehicleId(carDto.getVehicleId());
        if (null == carStatisticsDto) {
            //无数据 查询车型信息
            VehicleNameBizDto vehicleNameBizDto = vehicleBizService.getCarByModel(carDto.getVehicleId());
            if (null == vehicleNameBizDto) {
                throw new Exception("非法入参");
            }
            CarStatisticsCreateDto carStatisticsCreateDto = new CarStatisticsCreateDto();
            carStatisticsCreateDto.setCarId(carId);
            carStatisticsCreateDto.setTradingVolume(1);
            carStatisticsCreateDto.setVehicleId(carDto.getVehicleId());
            carStatisticsCreateDto.setVehicleName(vehicleNameBizDto.getModelName());
            carStatisticsCreateDto.setBrandName(vehicleNameBizDto.getBrandName());
            carStatisticsService.createCarStatistics(carStatisticsCreateDto);
        } else {
            //有数据，更新数量
            carStatisticsService.modifyTradingVolumeByVehicleId(carDto.getVehicleId());
        }

        return true;
    }
}
